<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\adminuser\controller;

class IndexUser extends \app\ApiRest
{
    protected $model;
    protected $admin_user;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\adminuser\model\AdminUser();
        $dis[] = ["user_id", "=", $this->getUserId()];
        $this->admin_user = $this->model->dataInfo($dis);
        if(empty($this->admin_user)) {
            $this->errorMsg("你不是管理员");
        }
    }
    public function index()
    {
        $comm_model = new \app\massage\model\Commission();
        $wallet_model = new \app\massage\model\Wallet();
        $data = $this->admin_user;
        $data["total_cash"] = $comm_model->where(["status" => 2, "type" => 16])->sum("company_cash");
        $data["wallet_cash"] = $wallet_model->where(["type" => 11])->where("status", "in", [2])->sum("total_price");
        return $this->success($data);
    }
    public function applyWallet()
    {
        $input = $this->_input;
        if(empty($input["apply_price"]) || $input["apply_price"] < 0) {
            $this->errorMsg("提现费最低一分");
        }
        if($this->admin_user["cash"] < $input["apply_price"]) {
            $this->errorMsg("余额不足");
        }
        $tax_point = getConfigSetting($this->_uniacid, "tax_point");
        $balance = 100 - $tax_point;
        $key = "adminuser_wallet";
        incCache($key, 1, $this->_uniacid);
        $value = getCache($key, $this->_uniacid);
        if($value != 1) {
            decCache($key, 1, $this->_uniacid);
            $this->errorMsg("网络错误，请刷新重试");
        }
        \think\facade\Db::startTrans();
        $cash = $input["apply_price"];
        $water_model = new \app\massage\model\CompanyWater();
        $res = $water_model->updateCash($this->_uniacid, 0, $cash, 0);
        if($res != 1) {
            \think\facade\Db::rollback();
            decCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "coach_id" => $this->admin_user["id"], "admin_id" => 0, "total_price" => $input["apply_price"], "balance" => $balance, "apply_price" => round($input["apply_price"] * $balance / 100, 2), "service_price" => round($input["apply_price"] * $tax_point / 100, 2), "code" => orderCode(), "tax_point" => $tax_point, "text" => $input["text"], "type" => 11, "last_login_type" => $this->is_app, "apply_transfer" => !empty($input["apply_transfer"]) ? $input["apply_transfer"] : 0];
        $wallet_model = new \app\massage\model\Wallet();
        $res = $wallet_model->dataAdd($insert);
        if($res != 1) {
            \think\facade\Db::rollback();
            decCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        \think\facade\Db::commit();
        decCache($key, 1, $this->_uniacid);
        return $this->success($res);
    }
    public function walletList()
    {
        $wallet_model = new \app\massage\model\Wallet();
        $input = $this->_param;
        $dis = ["type" => 11];
        if(!empty($input["status"])) {
            $dis["status"] = $input["status"];
        }
        $data = $wallet_model->dataList($dis, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
            }
        }
        $data["extract_total_price"] = $wallet_model->where(["type" => 11])->where("status", "in", [2])->sum("total_price");
        return $this->success($data);
    }
    public function updateCoachCashList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        if(!empty($input["coach_id"])) {
            $dis[] = ["coach_id", "=", $input["coach_id"]];
        }
        $dis[] = ["type", "=", 8];
        $record_model = new \app\massage\model\CashUpdateRecord();
        if(!empty($input["name"])) {
            $id = $record_model->getDataByTitle($input["name"]);
            $dis[] = ["id", "in", $id];
        }
        $admin_model = new \app\massage\model\Admin();
        $data = $record_model->dataList($dis, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $v["create_user"] = $admin_model->where(["id" => $v["create_user"]])->value("username");
                $v["user_name"] = $record_model->getUpdateObjTitle($v["coach_id"], $v["type"]);
            }
        }
        return $this->success($data);
    }
}

?>